﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for Wire.xaml
    /// </summary>
    public partial class Wire : UIElement
    {
        private Line line = new Line();
        private List<ExternalConnector> listeners = new List<ExternalConnector>();
        public Wire()
        {
            InitializeComponent();
        }
        public void writeWire(ExternalConnector sender, long data) { 
            foreach(ExternalConnector i in listeners){
                if( i!= sender) i.wireCallback(data);
            }
        }
        public void registerListener(ExternalConnector x)
        {
            if (listeners.Count == 2) throw new Exception("don't know how to act with more than 2 connecting elems");
            listeners.Add(x);
            if (listeners.Count == 2) drawConnection(); //draw a line
                
        }
        public void inregisterListener(ExternalConnector x)
        { 
            listeners.Remove(x);
        }
        public int countListeners() {
            return listeners.Count();
        }
        private void drawConnection() { // Needs to be rewritten!


          /*   Don't know how to implement this
            Line l = new Line();

            DependencyObject s = (DependencyObject)listeners[0];
             //s = Window1.getHighLevelUIElement(s);
            //while (VisualTreeHelper.GetParent(HLElem = VisualTreeHelper.GetParent(HLElem)) != (DependencyObject)  ) ;
             UIElement sz = (UIElement)VisualTreeHelper.GetParent(s);
            

            double xxxs = (double)sz.GetValue(Canvas.LeftProperty);
            double dgd = (double)sz.GetValue(Canvas.ActualWidthProperty);
            double a = (double)sz.GetValue(Canvas.LeftProperty) + (double)sz.GetValue(Canvas.ActualWidthProperty) / 2;
            double b = (double)sz.GetValue(Canvas.TopProperty) + (double)sz.GetValue(Canvas.ActualHeightProperty) / 2;

            l.X1 = a;
            l.Y1 = b;


            DependencyObject ss = (DependencyObject)listeners[1];
            //s = Window1.getHighLevelUIElement(s);
            ss = VisualTreeHelper.GetParent(ss);
            ss = VisualTreeHelper.GetParent(ss);
            double a1 = (double)ss.GetValue(Canvas.LeftProperty);
            double b1 = (double)ss.GetValue(Canvas.TopProperty);

            l.X2 = a1;
            l.Y2 = b1;
            l.Stroke = Brushes.Black;

            Canvas ca = (Canvas)VisualTreeHelper.GetParent(ss);
            ca.Children.Add(l);
*/
            
        }

    }
}
